import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_row_title.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_time_period.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/alarm/all_alarm_records_entity.dart';
import 'package:enterprise_v6_app/app/modules/alarm_management/alarm_management_controller.dart';

/// 报警管理页面
class AlarmManagementPage extends StatelessWidget {
  AlarmManagementPage({super.key});

  final controller = Get.put(AlarmManagementController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: controller.scaffoldKey,
      backgroundColor: AppColors.bg_color_grey,
      appBar: CustomAppBar(
        titleText: '报警管理',
      ),
      endDrawer: Container(
        width: AppDimens.sw - 80.w,
        height: AppDimens.sh,
        color: Colors.white,
        child: _filterDialog(),
      ),
      body: DefaultTabController(
        length: 2,
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.symmetric(horizontal: 6.w),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
              ),
              height: AppDimens.dp54,
              child: TabBar(
                tabs: const [
                  Tab(text: '未处置'),
                  Tab(text: '已处置'),
                ],
                controller: controller.tabController,
                dividerColor: Colors.transparent,
                labelPadding: EdgeInsets.only(bottom: 10.w, left: 6.w, right: 6.w),
                labelStyle: TextStyle(
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w500,
                ),
                unselectedLabelStyle: TextStyle(
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w500,
                ),
                labelColor: Colors.black,
                unselectedLabelColor: const Color(0xffA0A7B5),
                indicatorPadding: EdgeInsets.only(top: 36.w, bottom: 10.w),
                indicator: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(
                      AppImages.icon_app_work_plan,
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
                child: Container(
              color: const Color(0xffF5F5F6),
              child: TabBarView(
                controller: controller.tabController,
                children: [
                  _buildList(0),
                  _buildList(1),
                ],
              ),
            )),
          ],
        ),
      ),
    );
  }

  // 数据列表 1: 已处置 0: 未处置
  Widget _buildList(int i) {
    return Container(
      color: AppColors.bg_color_grey,
      padding: EdgeInsets.only(
        left: 16.w,
        right: 16.w,
      ),
      child: Column(
        children: [
          AppGaps.vGap14,
          Row(
            children: [
              Expanded(
                child: CustomSearchEnter(
                  height: 38.w,
                  hintText: '请输入设备编号',
                  text: controller.deviceCode,
                  onCallBack: (v) {
                    controller.deviceCode = v;
                  },
                ),
              ),
              AppGaps.hGap8,
              _screen(),
            ],
          ),
          AppGaps.vGap16,
          Expanded(
            child: CustomEasyRefreshList<AllAlarmRecordsEntity>(
              key: i == 1 ? controller.yesRefreshIndicatorKey : controller.noRefreshIndicatorKey,
              dataLoad: (pageNum, pageSize, {search}) {
                return controller.getAlarmList(pageNum, pageSize, disposalStatus: i);
              },
              itemBuilder: (item, index) {
                return CustomTitleChartPage(
                  title: '报警类型: ${item.alarmType}',
                  child: Column(
                    children: [
                      CustomRowTitle(
                        title: '报警等级',
                        content: item.alarmLevel,
                        contentColor: Colors.red,
                      ),
                      CustomRowTitle(
                        title: '设备编号',
                        content: '${item.numberCode}',
                      ),
                      CustomRowTitle(
                        title: '报警时间',
                        content: '${item.alarmTime}',
                      ),
                    ],
                  ),
                ).click(() {
                  if (i == 1) {
                    controller.toDisposed(item);
                  } else {
                    controller.toNotDispose(item);
                  }
                });
              },
            ),
          ),
        ],
      ),
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 32.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 18.w,
        height: 16.w,
      ),
    ).clickOnce(() {
      controller.scaffoldKey.currentState?.openEndDrawer();
    });
  }

  // 筛选弹框
  Widget _filterDialog() {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              //渐变位置
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              //渐变颜色[始点颜色, 结束颜色]
              colors: [
                Color(0xffE7EAFF),
                Colors.white,
                Colors.white,
                Colors.white,
              ],
            ),
          ),
          child: Column(
            children: [
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      AppGaps.vGap30,
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '筛选',
                          style: TextStyle(
                            fontSize: 18.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                      AppGaps.vGap20,
                      const Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '报警等级',
                        ),
                      ),
                      AppGaps.vGap12,
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Wrap(
                          children: controller.alarmLevelList.map((e) {
                            return Container(
                              margin: EdgeInsets.only(right: AppDimens.dp8),
                              child: ChoiceChip(
                                label: Text(e),
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(16.w),
                                ),
                                visualDensity: VisualDensity.compact,
                                selectedColor: AppColors.blue_button.withOpacity(0.5),
                                selected: controller.alarmLevel == e,
                                onSelected: (v) {
                                  controller.alarmLevel = e;
                                  controller.update();
                                },
                              ),
                            );
                          }).toList(),
                        ),
                      ),
                      AppGaps.vGap16,
                      const Align(alignment: Alignment.centerLeft, child: Text('报警类型')),
                      AppGaps.vGap12,
                      Wrap(
                        children: controller.alarmTypeList
                            .map(
                              (e) => Container(
                                margin: EdgeInsets.only(right: AppDimens.dp8),
                                child: ChoiceChip(
                                  label: Text(e.typeName ?? ''),
                                  visualDensity: VisualDensity.compact,
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(16.w),
                                  ),
                                  selectedColor: AppColors.blue_button.withOpacity(0.5),
                                  selected: controller.alarmType == e.typeName,
                                  onSelected: (v) {
                                    controller.alarmType = e.typeName;
                                    controller.update();
                                  },
                                ),
                              ),
                            )
                            .toList(),
                      ),
                      AppGaps.vGap16,
                      const Align(alignment: Alignment.centerLeft, child: Text('报警时间')),
                      AppGaps.vGap12,
                      CustomSelectTimePeriod(
                        startTime: controller.alarmStartTime ?? '',
                        endTime: controller.alarmEndTime ?? '',
                        onConfirm: (start, end) {
                          controller.alarmStartTime = start;
                          controller.alarmEndTime = end;
                        },
                        showBorder: true,
                        iconCalendar: AppImages.icon__screen_clock,
                      ),
                      AppGaps.vGap16,
                    ],
                  ),
                ),
              ),
              AppGaps.vGap16,
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  OutlinedButton(
                    onPressed: controller.reset,
                    child: Text(
                      '重置',
                      style: TextStyle(fontSize: 16.sp, color: AppColors.font_color_blue),
                    ),
                  ),
                  ElevatedButton(
                      style: ButtonStyle(backgroundColor: MaterialStateProperty.all(AppColors.font_color_blue)),
                      onPressed: controller.confirm,
                      child: Text(
                        '查询',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                        ),
                      )),
                ],
              ),
            ],
          ),
        );
      },
    );
  }
}
